using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeadIcon : MonoBehaviour
{
    // Start is called before the first frame update
    private Camera camera;
    public string name;
    GameObject hero;
    float npcHeight;

    public Texture2D blood_red;
    public Texture2D blood_black;

    private int HP = 100;
    private int MaxHp = 100;
    void Start()
    {
        hero = gameObject;
        camera = Camera.main;

        float size_y = GetComponent<Collider>().bounds.size.y;
        float scal_y = transform.localScale.y;
        npcHeight = (size_y * scal_y);

        HP = (int)gameObject.GetComponent<Enemy>().HP;
        MaxHp = (int)gameObject.GetComponent<Enemy>().MaxHp;
    }

    public void SetHP(int hp)
    {
        HP = hp;
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnGUI()
    {
        Vector3 worldPosition = new Vector3(transform.position.x, transform.position.y + npcHeight, transform.position.z);
        Vector2 position = camera.WorldToScreenPoint(worldPosition);
        position = new Vector2(position.x, Screen.height - position.y);

        Vector2 bloodSize = GUI.skin.label.CalcSize(new GUIContent(blood_red));

        int blood_width = blood_red.width * HP / MaxHp;

        GUI.DrawTexture(new Rect(position.x - bloodSize.x / 2, position.y - bloodSize.y, bloodSize.x, bloodSize.y), blood_black);
        GUI.DrawTexture(new Rect(position.x - bloodSize.x / 2, position.y - bloodSize.y, blood_width, bloodSize.y), blood_red);

        Vector2 nameSize = GUI.skin.label.CalcSize(new GUIContent(name));

        GUI.Label(new Rect(position.x - nameSize.x, position.y - nameSize.y - bloodSize.y, nameSize.x, nameSize.y), name);
    }
}
